const http = require('http')
const fs = require('fs');
const urllib = require('url');

const server = http.createServer((req, res) => {
	//解决跨域问题
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "*");
	res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
	//设置响应编码
	res.setHeader("Content-Type", "application/json;charset=utf-8");
	const {
		url,
		method
	} = req;

	if (method == 'GET') {
		const params = urllib.parse(req.url, true).query;
		console.log(params);
		res.end("数据响应完成");
	} else {
		//用于暂存请求体的信息
		let data = '';
		//通过req的data事件监听函数，每当接收到请求体的数据，就累加到data变量中
		req.on('data', chunk => {
			data += chunk;
		})
		//在end事件触发后，获取到了所有的请求数据
		req.on('end', () => {
			console.log(data);
			res.end("数据响应完成");
		})
	}
})

server.listen(8083, () => console.log("server running 8083"));
